package com.hesicare.adapter.worker.pull;

import com.hesicare.adapter.entity.*;
import com.hesicare.health.entity.SysBloodGlucose;
import com.hesicare.untils.StringUtil.StringUtil;
import com.hesicare.untils.dateUtil.DateUtil;
import com.hesicare.untils.httpUtils.okHttpUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 获取医院基本数据
 *
 * @Author 55097
 * @create 2021/11/18 19:54
 */
@Component
public class PullOnWorker {

    private final static Logger log = LoggerFactory.getLogger(PullOnWorker.class);


    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    @Resource(name = "jdbcTemplateHis")
    private JdbcTemplate jdbcTemplate;

    public Object readPotherList(String param) {
        List<HisPrescriptionOther> result = new ArrayList<HisPrescriptionOther>();
        // 查询医嘱，未开始的医嘱不同步！！！！
        StringBuffer sql = new StringBuffer()
                .append("sql");
        result = this.jdbcTemplate.query(sql.toString(), new RowMapper<HisPrescriptionOther>() {
            @Override
            public HisPrescriptionOther mapRow(ResultSet rs, int rowNum) throws SQLException {
                HisPrescriptionOther hisPrescriptionOther = new HisPrescriptionOther();
                return hisPrescriptionOther;
            }
        });
        return result;
    }

    public Object readPatientList(String param) {
        List<HisPatient> result = new ArrayList<HisPatient>();
        StringBuffer sql = new StringBuffer().append("sql");
        result = this.jdbcTemplate.query(sql.toString(), new RowMapper<HisPatient>() {
            @Override
            public HisPatient mapRow(ResultSet rs, int rowNum) throws SQLException {
                HisPatient hisPatient = new HisPatient();
                return hisPatient;
            }
        });
        return result;
    }

    public Object readUserList(String param) {
        List<HisStaff> result = new ArrayList<HisStaff>();
        StringBuffer sql = new StringBuffer().append("sql");
        result = this.jdbcTemplate.query(sql.toString(), new RowMapper<HisStaff>() {
            @Override
            public HisStaff mapRow(ResultSet rs, int rowNum) throws SQLException {
                HisStaff staff = new HisStaff();
                return staff;
            }
        });
        return result;
    }

    public Object readOutPatientList(String param) {
        List<HisPatient> result = new ArrayList<HisPatient>();
        StringBuffer sql = new StringBuffer().append("sql");
        result = this.jdbcTemplate.query(sql.toString(), new RowMapper<HisPatient>() {
            @Override
            public HisPatient mapRow(ResultSet rs, int rowNum) throws SQLException {
                HisPatient hisPatient = new HisPatient();
                return hisPatient;
            }
        });
        return result;
    }

    public Object readDeptList(String param) {
        List<HisDeptLevel> result = new ArrayList<HisDeptLevel>();
        StringBuffer sql = new StringBuffer().append("sql");
        result = this.jdbcTemplate.query(sql.toString(), new RowMapper<HisDeptLevel>() {
            @Override
            public HisDeptLevel mapRow(ResultSet rs, int rowNum) throws SQLException {
                HisDeptLevel hisDeptLevel = new HisDeptLevel();
                return hisDeptLevel;
            }
        });
        return result;
    }

    public void readClearPatientList(Set patientSet) {
        List<String> result = new ArrayList<String>();
        StringBuffer sql = new StringBuffer().append("sql");
        this.jdbcTemplate.query(sql.toString(), new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                patientSet.add("患者信息");
                return null;
            }
        });
    }

    public boolean recv(SysBloodGlucose bloodGlucose) {
        String reqMsg = null;
        String respMsg = null;
        if (null != bloodGlucose) {
            Long id = bloodGlucose.getId();
            String value = String.valueOf(bloodGlucose.getMeatureValue());
            String status = bloodGlucose.getStatus();
            Date measureTime = bloodGlucose.getMeasureTime();
            String deptCode = bloodGlucose.getLeader();
            String hisPatientId = bloodGlucose.getHisPatientId();
            String hospitalNum = bloodGlucose.getHospitalizedNum();
            String hospitalTimes = bloodGlucose.getLoginName();
            String patientName = bloodGlucose.getPatientName();
            String hisCode = bloodGlucose.getHisCode();
            Long patientId = bloodGlucose.getPatientId();
            String opratorId = bloodGlucose.getHisUserId();
            String opratorName = bloodGlucose.getUserName();
            String testDate = DateUtil.dateTime(measureTime);
            String testTime = DateUtil.dateAllTime(measureTime);
            String createBy = bloodGlucose.getCreateBy();
            Long deptId = bloodGlucose.getDeptId();
            String testIsNormal = null;
            String testState = null;
            String message = null;
            String otherRemark = bloodGlucose.getOtherRemark();
            String resultType = bloodGlucose.getMeasurePeriod();
            String manufacture = bloodGlucose.getManufacture();
            switch (value) {
                case "990.0":
                    testIsNormal = "Lo";
                    break;
                case "991.0":
                    testIsNormal = "HI";
                    break;
                case "996.0":
                    testIsNormal = "食晚";
                    break;
                case "997.0":
                    testIsNormal = "外出";
                    break;
                case "999.0":
                    if (StringUtil.isNotBlank(otherRemark)) {
                        testIsNormal = otherRemark;
                    } else {
                        testIsNormal = "其他";
                    }
                    value = "";
                    break;
                case "998.0":
                    value = "";
                    testIsNormal = "拒测";
                    break;
                default:
                    testIsNormal = "";
                    break;
            }
            respMsg = okHttpUtil.builder().url("接口地址")
                    .addHeader("属性", "参数")
                    .addHeader("头文件", "参数")
                    .get()
                    .async();
            log.info("血糖推送记录" + reqMsg);
            log.info("血糖id:" + id + "结果:" + respMsg);
        }
        return true;
    }
}
